const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function getExpressionParametersQuery(req, res, next) {
  logger.winston.info('parameters.getExpressionParametersQuery');
  const expressionIds = req.query.expression_ids;
  var array_expressionIds = expressionIds.split(","); //Very hacky due to the node-sqlite not supporting IN from an array
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from expression_parameters inner join entities on expression_parameters.entity_id = entities.entity_id where expression_id  in (" + array_expressionIds + ")  order by expression_id desc");
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }     
  
  

  
}

async function getIntentParameters(req, res, next) {
  logger.winston.info('parameters.getIntentParameters');

  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from expression_parameters where intent_id = ?",[req.params.intent_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  } 
    
  
}

async function getExpressionParameters(req, res, next) {
  logger.winston.info('parameters.getExpressionParameters');

  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from expression_parameters where expression_id = ?",[req.params.expression_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }   
  
  
}

async function createExpressionParameter(req, res, next) {
  logger.winston.info('parameters.createExpressionParameter');
 
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into expression_parameters(expression_id, parameter_start, parameter_end, parameter_value, intent_id) values (?,?,?,?,?)", [req.body.expression_id, req.body.parameter_start, req.body.parameter_end, req.body.parameter_value, req.body.intent_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
	    
  
}

async function updateParameter(req, res, next) {
  //Sets the entity once a new parameter has been created
  logger.winston.info('parameters.updateParameter');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("update expression_parameters set entity_id = ? where parameter_id = ?", [req.body.entity_id, req.body.parameter_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }    
  
}

async function removeExpressionParameter(req, res, next) {

    logger.winston.info('parameters.removeExpressionParameter');
    
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from expression_parameters where parameter_id = ?", [req.body.parameter_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }    
  
}

module.exports = {
  getExpressionParameters,
  getIntentParameters,
  createExpressionParameter,
  removeExpressionParameter,
  updateParameter,
  getExpressionParametersQuery
};
